package com.li.client.management;

import com.google.gwt.dom.client.Element;

public class DynamicCallbackFormImpl {
	/**
	 * Gets the response html from the loaded iframe.
	 * 
	 * @param iframe
	 *            the iframe from which the response html is to be extracted
	 * @return the response html
	 */
	public native String getContents(Element iframe) /*-{
														try {
														// Make sure the iframe's window & document are loaded.
														if (!iframe.contentWindow || !iframe.contentWindow.document)
														return null;

														// Get the body's entire inner HTML.
														return iframe.contentWindow.document.body.innerHTML;
														} catch (e) {
														return null;
														}
														}-*/;

	/**
	 * Gets the form element's encoding.
	 * 
	 * @param form
	 *            the form whose encoding is to be retrieved
	 * @return the form's encoding type
	 */
	public native String getEncoding(Element form) /*-{
													// We can always get 'enctype', no matter which browser, because we set
													// both 'encoding' and 'enctype' in setEncoding().
													return form.enctype;
													}-*/;

	/**
	 * Hooks the iframe's onLoad event and the form's onSubmit event.
	 * 
	 * @param iframe
	 *            the iframe whose onLoad event is to be hooked
	 * @param form
	 *            the form whose onSubmit event is to be hooked
	 * @param listener
	 *            the listener to receive notification
	 */
	public native void hookEvents(Element iframe, final DynamicCallbackFormImplHost listener) /*-{
																								if (iframe) {
																								iframe.onload = function() {
																								try{
																								// If there is no __formAction yet, this is a spurious onload
																								// generated when the iframe is first added to the DOM.
																								if (!iframe.__formAction)
																								return;

																								listener.@com.li.client.management.DynamicCallbackFormImplHost::onFrameLoad()();
																								}catch(e){
																								alert("Error"+e);
																								}
																								};
																								
																								//  alert(iframe.onload);
																								}

																								}-*/;

	/**
	 * Submits a form.
	 * 
	 * @param form
	 *            the form to be submitted
	 * @param iframe
	 *            the iframe that is targetted, or <code>null</code>
	 */
	public native void submit(String action, Element iframe) /*-{
																// Hang on to the form's action url, needed in the
																// onload/onreadystatechange handler.
																// alert(iframe+"  action: "+action);
																if (iframe)
																iframe.__formAction = action;
																//alert("Set: " +iframe.__formAction);  
																}-*/;

	/**
	 * Unhooks the iframe's onLoad event.
	 * 
	 * @param iframe
	 *            the iframe whose onLoad event is to be unhooked
	 * @param form
	 *            the form whose onSubmit event is to be unhooked
	 */
	public native void unhookEvents(Element iframe) /*-{
													if (iframe)
													iframe.onload = null;
													}-*/;
}
